package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
@Entity
@Table(name="customer")
public class Customer extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	
	public String name; //客户名称
	
	public String contact; //联系人
	
	public String phone; //联系电话
	
	public String email; //邮箱
	
	public String address; //客户地址
	
	public String type; //客户类型
	
	public String image; //照片
	public static Model.Finder<Integer, Customer> find = new Model.Finder<Integer, Customer>(Integer.class, Customer.class);

	public static Page<Customer> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Customer> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setName(String d) {
		name= d;
	}
	public String getName() {
		return name;
	}
	public void setContact(String d) {
		contact= d;
	}
	public String getContact() {
		return contact;
	}
	public void setPhone(String d) {
		phone= d;
	}
	public String getPhone() {
		return phone;
	}
	public void setEmail(String d) {
		email= d;
	}
	public String getEmail() {
		return email;
	}
	public void setAddress(String d) {
		address= d;
	}
	public String getAddress() {
		return address;
	}
	public void setType(String d) {
		type= d;
	}
	public String getType() {
		return type;
	}
	public void setImage(String d) {
		image= d;
	}
	public String getImage() {
		return image;
	}
	public static Customer findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Customer v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Customer findByName(String d) {
		return find.where().eq("name", d).findUnique();
	}
	public static String getNameJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Customer v: find.all()) {
			node.put(v.getName().toString(), v.getName().toString()); 
		}
		return node.toString();
	}
	public static Customer findByContact(String d) {
		return find.where().eq("contact", d).findUnique();
	}
	public static String getContactJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Customer v: find.all()) {
			node.put(v.getContact().toString(), v.getContact().toString()); 
		}
		return node.toString();
	}
	public static Customer findByPhone(String d) {
		return find.where().eq("phone", d).findUnique();
	}
	public static String getPhoneJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Customer v: find.all()) {
			node.put(v.getPhone().toString(), v.getPhone().toString()); 
		}
		return node.toString();
	}
	public static Customer findByEmail(String d) {
		return find.where().eq("email", d).findUnique();
	}
	public static String getEmailJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Customer v: find.all()) {
			node.put(v.getEmail().toString(), v.getEmail().toString()); 
		}
		return node.toString();
	}
	public static Customer findByAddress(String d) {
		return find.where().eq("address", d).findUnique();
	}
	public static String getAddressJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Customer v: find.all()) {
			node.put(v.getAddress().toString(), v.getAddress().toString()); 
		}
		return node.toString();
	}
	public static Customer findByType(String d) {
		return find.where().eq("type", d).findUnique();
	}
	public static String getTypeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Customer v: find.all()) {
			node.put(v.getType().toString(), v.getType().toString()); 
		}
		return node.toString();
	}
}
